using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using System.Web.Handlers;

namespace GridControl.Helpers
{   
    /// <summary>
    /// Class To serialize the objects to JSon
    /// </summary>
    public static class DataParserHelper
    {

       
        /// <summary>
        /// Parse data of the FIRST table in the given DataSet.
        /// </summary>
        /// <param name="ds">DataSet to parse</param>
        /// <param name="fields">Columns to extract from Table</param>
        /// <returns>Returns a JS array string</returns>
        public static string ParseData(DataSet ds, string fields)
        {
            if (ds.Tables.Count == 0)
                return string.Empty;

            return ParseData(ds.Tables[0], fields);
        }
       
        /// <summary>
        /// Parse data of the given DataTable.
        /// </summary>
        /// <param name="dt">DataTable to parse</param>
        /// <param name="fields">Columns to extract from Table</param>
        /// <returns>Returns a Json string</returns>
        public static string ParseData(DataTable dt, string fields)
        {
            //Modified by rodrigo -returns a Json string to the grid (working)

            if (dt.Rows.Count == 0)
                return string.Empty;

            string[] columns = fields.Split(',');
            string rows = string.Empty;
            JavaScriptSerializer ser = new JavaScriptSerializer();
            foreach (DataRow dr in dt.Rows)
            {

                rows += "{";
                for (int indxCol = 0; indxCol < columns.Length; indxCol++)
                {
                    string value = dr.IsNull(indxCol) ? string.Empty : dr[indxCol].ToString();
                    rows += ser.Serialize(columns[indxCol])+":"+ser.Serialize(value); //"\""+columns[indxCol]+"\":\"" + ser.Serialize(dr[indxCol]) + "\"";
                    if (indxCol != columns.Length - 1)
                    {
                        rows += ",";
                    }
                    // row = string.Concat(row, value, ",");
                }
                rows += "},";
            }
            rows = rows.Remove(rows.Length - 1);
           

            return rows;
        }

        /// <summary>
        /// Parse data of the given IEnumerable object.
        /// </summary>
        /// <param name="enumerable">IEnumerable object to parse</param>
        /// <param name="fields">Columns to extract from Table</param>
        /// <returns>Returns a JS array string</returns>
        public static string ParseData(System.Collections.IEnumerable enumerable, string fields)
        {
            //return string.Empty;
            string[] columns = fields.Split(',');

            System.Collections.IEnumerator enu = enumerable.GetEnumerator();
            enu.Reset();
            enu.MoveNext();
            System.Type type = enu.Current.GetType();
            enu.Reset();

            string rows = "{";
            JavaScriptSerializer ser = new JavaScriptSerializer();
            while (enu.MoveNext())
            {
                if (enu.Current == null) continue;

                
                for (int indxCol = 0; indxCol < fields.Length; indxCol++)
                {
                    string value = string.Empty;
                    PropertyInfo pi = type.GetProperty(columns[indxCol]);
                    if (pi.PropertyType.IsPrimitive || pi.PropertyType == typeof(string) || pi.PropertyType == typeof(DateTime))
                        value = pi.GetValue(enu.Current, null).ToString();

                    rows += ser.Serialize(columns[indxCol]) + ":" + ser.Serialize(value); //"\""+columns[indxCol]+"\":\"" + ser.Serialize(dr[indxCol]) + "\"";
                    if (indxCol != columns.Length - 1)
                    {
                        rows += ",";
                    }
                }
               
            }
            rows = string.Concat(rows.Substring(0, rows.Length - 1), "}");

            return rows;
        }
    }
    
}
